//
//  DPViewController.m
//  DPTubeStatus
//
//  Created by Joel Parsons on 22/11/2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DPViewController.h"
#import "DPTubeLine.h"
#import "DPTubeNetwork.h"
#import "DPTubeLineDetailController.h"


@implementation DPViewController

@synthesize tableView;
@synthesize tubeNetwork, tubeLineOrderArray;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.tubeNetwork = [[DPTubeNetwork alloc] init];
    [self.tubeNetwork getTubeLineStatus];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    
    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    if (indexPath != nil) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (IBAction)refreshButtonTapped:(id)sender {
    [self.tubeNetwork getTubeLineStatus];
    [self.tableView reloadData];
    
}

- (IBAction)editButtonTapped:(id)sender {
    if ([self.tableView isEditing]) {
        [self.tableView setEditing:NO animated:YES];
        UIBarButtonItem * aButton = (UIBarButtonItem *)sender;
        aButton.style = UIBarButtonItemStyleBordered;
        
    }
    else {
        [self.tableView setEditing:YES animated:YES];
        UIBarButtonItem * aButton = (UIBarButtonItem *)sender;
        aButton.style = UIBarButtonItemStyleDone;
    }
    
}


#pragma mark -
#pragma mark Property Methods

-(NSMutableArray *)tubeLineOrderArray{
    if (!tubeLineOrderArray) {
        self.tubeLineOrderArray = [[NSMutableArray alloc] init];
        
        for (DPTubeLine * aTubeLine in self.tubeNetwork.tubeLineArray) {
            [tubeLineOrderArray addObject:aTubeLine.name];
        }
    }
    
    return tubeLineOrderArray;
}

#pragma mark -
#pragma mark Table View Delegate Methods


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * tubeLineString = [self.tubeLineOrderArray objectAtIndex:indexPath.row];
    DPTubeLine * aTubeLine = [self.tubeNetwork.tubeLineDictionary objectForKey:tubeLineString];
    
    DPTubeLineDetailController * detailController = [[DPTubeLineDetailController alloc] initWithNibName:@"DPTubeLineDetailController" bundle:nil];
    
    detailController.tubeLine = aTubeLine;
    
    [self.navigationController pushViewController:detailController animated:YES];
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tubeNetwork.tubeLineArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"tubeLineCell";

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString * tubeLineString = [self.tubeLineOrderArray objectAtIndex:indexPath.row];
    DPTubeLine * aTubeLine = [self.tubeNetwork.tubeLineDictionary objectForKey:tubeLineString] ;
    
    cell.textLabel.text = aTubeLine.name;
    cell.detailTextLabel.text = aTubeLine.statusCode;
    
    NSString * iconPath = [[NSBundle mainBundle] pathForResource:@"LTimage" ofType:@"jpg"];
    UIImage * iconImage = [UIImage imageWithContentsOfFile:iconPath];
    
    cell.imageView.image = iconImage;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}



- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    
    NSString * aString = [self.tubeLineOrderArray objectAtIndex:fromIndexPath.row];
    
    [self.tubeLineOrderArray removeObjectAtIndex:fromIndexPath.row];
    [self.tubeLineOrderArray insertObject:aString atIndex:toIndexPath.row]; 
    
    [self.tableView reloadData];
}


@end
